<?php
class Permissions extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->Model(array("Mpermissions"));
		$this->breadcrumbs->push('Permissions', 'admincp/permissions/view');
	}
	
	public function view(){
        $data['icon_page']=$this->config->item('icon_view');
        $data['title']='Permissions Manager';
        $data['sub_title']='Permissions Manager';
        $data['columns'] = array(
        	'id' => trans('id'),
        	'key' => trans('key'),
        	'name' => trans('title'),
        	'description' => trans('description'),
        	'group_id' => trans('group'),
        	'order' => trans('order')
        );
        $data['permissions']=$this->Mpermissions->listPermission(array_keys($data['columns']));
		$this->parser->parse("permissions/view.tpl", NULL, $data);
	}

	public function add(){
		$data['icon_page']=$this->config->item('icon_create');
        $data['title']='Create New Roles';
        $data['sub_title']='Create New Roles';
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$data_insert = array(
				'key' => $this->input->post('key'),
				'name' => $this->input->post('name'),
				'description' => $this->input->post('description'),
				'group' => $this->input->post('group'),
				'order' => $this->input->post('order')
			);
			$this->Mpermissions->insert($data_insert);
			$this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
			redirect(admincp_url()."permissions/view");		
		}
		$this->breadcrumbs->push('Create Permission', 'admincp/permissions/add');
		$this->parser->parse("permissions/add.tpl", NULL, $data);
	}

	public function edit(){
		$data['icon_page']=$this->config->item('icon_edit');
        $data['title_page']='Edit Permission';
        $data['sub_title_page']='Edit Permission';
		$cache_id = $id = $this->uri->segment(4);
		$data['info'] = $this->Mpermissions->get_data($id);
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$data_update = array(
				'key' => $this->input->post('key'),
				'name' => $this->input->post('name'),
				'description' => $this->input->post('description'),
				'group' => $this->input->post('group'),
				'order' => $this->input->post('order')
			);
			$this->Mpermissions->update($data_update,$id);
			$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
			redirect(admincp_url()."permissions/view");
		}
		$this->breadcrumbs->push('Edit Permission', 'admincp/permissions/edit/'.$id);
		$this->parser->parse("permissions/edit.tpl", $cache_id, $data);
	}

	public function delete(){
		$id = $this->uri->segment(4);
		$this->Mpermissions->delete($id);
		$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		redirect(admincp_url()."permissions/view");
	}
	
}